package Elasticsearchs

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/Elasticsearchs"
	ElasticsearchsReq "github.com/flipped-aurora/gin-vue-admin/server/model/Elasticsearchs/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
)

type InterviewEsService struct {
}

// CreateInterviewEs 创建interview表记录
// Author [piexlmax](https://github.com/piexlmax)
func (interviewEsService *InterviewEsService) CreateInterviewEs(interviewEs *Elasticsearchs.InterviewEs) (err error) {
	err = global.GVA_DB.Create(interviewEs).Error
	return err
}

// DeleteInterviewEs 删除interview表记录
// Author [piexlmax](https://github.com/piexlmax)
func (interviewEsService *InterviewEsService) DeleteInterviewEs(interviewEs Elasticsearchs.InterviewEs) (err error) {
	err = global.GVA_DB.Delete(&interviewEs).Error
	return err
}

// DeleteInterviewEsByIds 批量删除interview表记录
// Author [piexlmax](https://github.com/piexlmax)
func (interviewEsService *InterviewEsService) DeleteInterviewEsByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]Elasticsearchs.InterviewEs{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateInterviewEs 更新interview表记录
// Author [piexlmax](https://github.com/piexlmax)
func (interviewEsService *InterviewEsService) UpdateInterviewEs(interviewEs Elasticsearchs.InterviewEs) (err error) {
	err = global.GVA_DB.Save(&interviewEs).Error
	return err
}

// GetInterviewEs 根据id获取interview表记录
// Author [piexlmax](https://github.com/piexlmax)
func (interviewEsService *InterviewEsService) GetInterviewEs(id uint) (interviewEs Elasticsearchs.InterviewEs, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&interviewEs).Error
	return
}

// GetInterviewEsInfoList 分页获取interview表记录
// Author [piexlmax](https://github.com/piexlmax)
func (interviewEsService *InterviewEsService) GetInterviewEsInfoList(info ElasticsearchsReq.InterviewEsSearch) (list []Elasticsearchs.InterviewEs, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&Elasticsearchs.InterviewEs{})
	var interviewEss []Elasticsearchs.InterviewEs
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&interviewEss).Error
	return interviewEss, total, err
}
